import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ReaderDAO {
    // 查询所有读者
    public List<Reader> getAllReaders() {
        List<Reader> readers = new ArrayList<>();
        String sql = "SELECT * FROM readers";
        try (Connection conn = DatabaseConnection.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Reader reader = new Reader();
                reader.setId(rs.getInt("id"));
                reader.setReaderId(rs.getString("reader_id"));
                reader.setName(rs.getString("name"));
                reader.setGender(rs.getString("gender"));
                reader.setPhone(rs.getString("phone"));
                reader.setEmail(rs.getString("email"));
                reader.setAddress(rs.getString("address"));
                readers.add(reader);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return readers;
    }

    // 根据 ID 查询读者
    public Reader getReaderById(int id) {
        String sql = "SELECT * FROM readers WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    Reader reader = new Reader();
                    reader.setId(rs.getInt("id"));
                    reader.setReaderId(rs.getString("reader_id"));
                    reader.setName(rs.getString("name"));
                    reader.setGender(rs.getString("gender"));
                    reader.setPhone(rs.getString("phone"));
                    reader.setEmail(rs.getString("email"));
                    reader.setAddress(rs.getString("address"));
                    return reader;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 更新读者信息
    public boolean updateReader(Reader reader) {
        String sql = "UPDATE readers SET reader_id = ?, name = ?, gender = ?, phone = ?, email = ?, address = ? WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, reader.getReaderId());
            pstmt.setString(2, reader.getName());
            pstmt.setString(3, reader.getGender());
            pstmt.setString(4, reader.getPhone());
            pstmt.setString(5, reader.getEmail());
            pstmt.setString(6, reader.getAddress());
            pstmt.setInt(7, reader.getId());
            int affectedRows = pstmt.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 添加读者
    public boolean addReader(Reader reader) {
        String sql = "INSERT INTO readers (reader_id, name, gender, phone, email, address) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, reader.getReaderId());
            pstmt.setString(2, reader.getName());
            pstmt.setString(3, reader.getGender());
            pstmt.setString(4, reader.getPhone());
            pstmt.setString(5, reader.getEmail());
            pstmt.setString(6, reader.getAddress());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("添加读者时发生 SQL 异常: " + e.getMessage());
            return false;
        }
    }
}